// Created by plusminus on 22:01:11 - 29.09.2008
package com.talkweb.osmharmony.views.overlay.mylocation;

import com.talkweb.osmharmony.ResourceTable;
import com.talkweb.osmharmony.util.GeoPoint;
import com.talkweb.osmharmony.util.ResourceHelper;
import com.talkweb.osmharmony.views.MapView;
import com.talkweb.osmharmony.views.Projection;
import com.talkweb.osmharmony.views.overlay.Overlay;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * @author Nicolas Gramlich
 */
public class SimpleLocationOverlay extends Overlay {
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================

    protected final Paint mPaint = new Paint();

    protected PixelMap PERSON_ICON;
    /**
     * Coordinates the feet of the person are located.
     */
    protected Point PERSON_HOTSPOT = new Point(24, 39);

    protected GeoPoint mLocation;
    private final Point screenCoords = new Point();

    // ===========================================================
    // Constructors
    // ===========================================================

    /**
     * Use SimpleLocationOverlay(PixelMap) SimpleLocationOverlay}(((BitmapDrawable)ctx.getResources().getDrawable(R.drawable.person)).getBitmap()) instead.
     */
    @Deprecated
    public SimpleLocationOverlay(final Context ctx) {
        this(new PixelMapElement(ResourceHelper.getResource(ctx,ResourceTable.Media_person)).getPixelMap());
    }

    public SimpleLocationOverlay(PixelMap theIcon) {
        super();
        this.PERSON_ICON = theIcon;
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public void setLocation(final GeoPoint mp) {
        this.mLocation = mp;
    }

    public GeoPoint getMyLocation() {
        return this.mLocation;
    }

    // ===========================================================
    // Methods from SuperClass/Interfaces
    // ===========================================================

    @Override
    public void onDetach(MapView mapView) {
        //https://github.com/osmdroid/osmdroid/issues/477
        //commented out to prevent issues
        //this.PERSON_ICON.recycle();
    }

    @Override
    public void draw(final Canvas c, final Projection pj) {
        if (this.mLocation != null) {
            pj.toPixels(this.mLocation, screenCoords);
            PixelMapHolder holder = new PixelMapHolder(PERSON_ICON);
            c.drawPixelMapHolder(holder, screenCoords.getPointX() - PERSON_HOTSPOT.getPointX(), screenCoords.getPointY()
                    - PERSON_HOTSPOT.getPointY(), this.mPaint);
        }
    }

    // ===========================================================
    // Methods
    // ===========================================================

    /**
     * Coordinates the feet of the person are located.
     */
    public void setPersonIcon(PixelMap bmp, Point hotspot) {
        this.PERSON_ICON = bmp;
        this.PERSON_HOTSPOT = hotspot;
    }
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}
